using LitJson;
using UnityEngine;
using UnityEngine.UI;
namespace CGF
{
    public partial class PanelVIPRule : UIComponentBase, IAwake<UILayer, bool>, IDestroy
    {
        private float[] discounts = new float[5];
        private int[] leaseMins = new int[5];

        public static async void Open()
        {
            var res = await Network.Get("/v1/config/cost");
            var instance = UIManagerComponent.instance.OpenUI<PanelVIPRule>(UILayer.Normal, RootComponent.instance, true);
            instance.SetProsBindCompletedAction(() =>
            {
                instance.SetInfo(res.jsonData);
            });
        }

        public static void Close()
        {
            UIManagerComponent.instance.CloseUI<PanelVIPRule>();
        }

        public async void Awake(UILayer layer, bool isFloat)
        {
            await SpawnPrefab("Prefabs/GameUI/PanelVIPRule", layer, isFloat);
            BindProperties(transform);

            BtnClose.SetClick(Close);

            LevelProgress.value = 0;
            ScaleIn(Dialog.transform);
        }
        
        private void SetInfo(JsonData resJson)
        {
            JsonData cellDatas = resJson["data"];
            for (int i = 0; i < leaseMins.Length; i++)
            {
                JsonData data = cellDatas[(i + 1).ToString()];
                discounts[i] = (int)data["duration"];
                leaseMins[i] = (int)((int)data["cost"] * 0.01f);
            }
            
            int level = (int)UserInfo.Info["level"];
            LevelProgress.value = level - 1;
            Color pointColor = Point0.color;
            Color textColor = Point0.gameObject.Get<Text>("TextTop").color;
            ImagePro[] points = { Point0, Point1, Point2, Point3, Point4 };
            Text[] descs = { Text0, Text1, Text2, Text3, Text4 };
            for (int i = 0; i < points.Length; i++)
            {
                points[i].color = level > i ? pointColor : Color.white;
                Text topText = points[i].gameObject.Get<Text>("TextTop");
                Text bottomText = points[i].gameObject.Get<Text>("TextBottom");
                topText.color = level > i ? textColor : Color.white;
                bottomText.color = level > i ? textColor : new Color(1, 1, 1, 0.5f);
                topText.text = $"{I18N_get_discount.Replace("{num}", discounts[i].ToString())}";
                bottomText.text = CommAssets.instance.vipNames[i + 1];

                descs[i].text = $"{CommAssets.instance.vipNames[i + 1]} - {I18N_lease_amount_to} {I18N.Currency} {leaseMins[i]}";
            }
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
        }

    }
}
